<?php

declare(strict_types=1);

namespace app\backend\controller\crm;

use app\common\controller\Basic;
use app\common\service\crm\OpenCustomerService;
use app\common\service\crm\RecordService;
use app\common\service\sys\ConfigService;

/**
 * 客户表 控制器
 * @author 吕玉冰
 * @date  2022-07-21
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class OpenCustomer extends Basic
{

    // 初始化
    protected function initialize()
    {
        parent::initialize();
        // 当前控制器服务实例化
        $this->service = new OpenCustomerService();
        $this->sysconfigService = new ConfigService();
        $this->recordService = new RecordService();
    }

    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json($this->service->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input('param.');
            if (!isset($data['allclose'])) {
                $this->_form_filter($data);
                $data['parent_id'] = 0;
                return json($this->service->saveForm($data));
            } else {
                return _jok('ok', []);
            }
        }
        if ($this->request->isGet()) {
            $data = [];
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = input('id/d', 0);
        if ($id === 0) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            $data = input('param.');
            $this->_form_filter($data);
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = $this->service->getInfoById($id);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 添加编辑-表单数据处理
     */
    protected function _form_filter(&$data)
    {
        $data = rq_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = input('id', '');
            if ($id === 0) {
                return _jerr(1002);
            }
            return json($this->service->_del($id));
        }
        return _jerr(1000);
    }

    public function show_address()
    {
        $data = [];
        $data['set'] = "AXGBZ-6VF33-SKG36-3BRXK-3K2QF-CCFOV";
        return _jok('ok', $data);
    }

    public function auto()
    {
        if ($this->request->isPost()) {
            //获取配置
            $where = [];
            $where[] = ["name", "like", "crm%"];
            $info = $this->sysconfigService->getListAll($where);
            $infos = [];
            foreach ($info as $key => $value) {
                $infos[$value["name"]] = $value['value'];
            }

            //未跟进多少天自动入公海,是否包含已经交易的客户
            $crm_sign_record_day = $infos['crm_sign_record_day']; // 多少天
            $crm_no_record = $infos['crm_no_record']; //没有跟进
            $crm_sign_record = $infos['crm_sign_record']; //没有签订

            //未交易多少天自动入公海
            $crm_no_deal = $infos['crm_no_deal'];
            $crm_no_deal_days = $infos['crm_no_deal_days'];

            $where = [];
            $where[] = ["parent_id", '<>', '0'];
            $total = $this->service->getTotal($where);

            $page = intval(input('page', 1));
            $limit = intval(input('limit', 20));
            $redata = $this->service->getAllCustomerInfo($page, $limit);
            $auto = false;
            foreach ($redata as $key => $value) {
                if ($crm_no_record == 1 && $crm_sign_record == 0 && ($value["follow_time"] == null || time() - $value["follow_time"] < 60 * 60 * 24 * $crm_sign_record_day)) {
                    $data = [];
                    $data['parent_id'] = 0;
                    $this->service->update($key, $data);
                    continue;
                }

                if ($crm_no_record == 1 && $crm_sign_record == 1 && $value["order_time"] == null && ($value["follow_time"] == null && time() - $value["follow_time"] > 60 * 60 * 24 * $crm_sign_record_day)) {
                    $data = [];
                    $data['parent_id'] = 0;
                    $this->service->update($key, $data);
                    continue;
                }

                if ($crm_no_deal == 1 && (time() - $value["order_time"] < 60 * 60 * 24 * $crm_no_deal_days || $value["order_time"] == null)) {
                    $data = [];
                    $data['parent_id'] = 0;
                    $this->service->update($key, $data);
                    continue;
                }
            }
            $redata = [];
            $redata['total'] = $total;
            if ($page * $limit <= $total) {
                $redata['page'] = $page + 1;
                return _jok("第{$page}自动分析完成!", $redata);
            } else {
                return _jok("自动分析完成!", $redata);
            }

        }
        return _jerr(1000);
    }

    public function set()
    {
        $id = input('id/d', 0);
        if ($id === 0) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            $data = [];
            $data['id'] = $id;
            $data['parent_id'] = 0;
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = $this->service->getInfoById($id);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    public function get()
    {
        $id = input('id/d', 0);
        if ($id === 0) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            $data = [];
            $data['id'] = $id;
            $data['parent_id'] = $this->user['id'];
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = $this->service->getInfoById($id);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }
}
